package colibri.effects;

import java.util.Timer;
import javax.microedition.lcdui.Display;
import javax.microedition.lcdui.Graphics;
import javax.microedition.lcdui.Image;
import javax.microedition.lcdui.game.GameCanvas;
import javax.microedition.midlet.MIDlet;


public class SliderCanvas extends GameCanvas implements AnimatedCanvas {
  GameCanvas destinationCanvas_;
  private MIDlet midlet_;
  private Image src_;
  private Image dst_;
  private int dir_;
  private int xDelta_;
  public final static int LEFT = 1;
  public final static int RIGHT = 2;
  private Rectangle srcRect_;
  private Rectangle dstRect_;
  private Timer t_;
  private static final int pixelsPerStep_ = 10;

  private int tbHeight_ = 0;





  
  /**
   * Creates a new instance of SliderCanvas
   */
  public SliderCanvas( MIDlet midlet, GameCanvas src, GameCanvas dst, int dir) {
    super(false);
    midlet_ = midlet;
    destinationCanvas_ = dst;
    src_ = Image.createImage(getWidth(), getHeight());
    dst_ = Image.createImage(getWidth(), getHeight());
    src.paint(src_.getGraphics());
    dst.paint(dst_.getGraphics());
    srcRect_ = new Rectangle(0, tbHeight_, getWidth(), getHeight() - tbHeight_);
    dstRect_ = new Rectangle(0, tbHeight_, getWidth(), getHeight() - tbHeight_);
    dir_ = dir;
    xDelta_ = 0;
    this.setFullScreenMode(true);
    initializeTimer();
  }





    private void initializeTimer() {
        t_ = new Timer();
        t_.schedule(new GameCanvasAnimationTimerTask(this), 10, 10);
    }





    public void animateStep() {
        if (xDelta_ + pixelsPerStep_ > getWidth()) {
            t_.cancel();
            Display.getDisplay(midlet_).setCurrent(destinationCanvas_);
        }
        xDelta_ += pixelsPerStep_;
        repaint();
    }





    public void paint(Graphics g) {
        Rectangle s;
        Rectangle d;
        if (dir_ == LEFT) {
            s = srcRect_.clipWidthFromLeft(xDelta_);
            d = dstRect_.clipWidthFromRight(getWidth() - xDelta_);
            if(s.w() > 0) {
                g.drawImage(Image.createImage(src_,
                                            s.x(),
                                            s.y(),
                                            s.w(),
                                            s.h(),
                                            0),
                          0,
                          0,
                          20);
            }
            if(d.w() > 0) {
                g.drawImage(Image.createImage(dst_,
                                            d.x(),
                                            d.y(),
                                            d.w(),
                                            d.h(),
                                            0),
                          getWidth() - xDelta_,
                          0,
                          20);
            }
        } else {
            s = srcRect_.clipWidthFromRight(xDelta_);
            d = dstRect_.clipWidthFromLeft(getWidth() - xDelta_);
            if(s.w() > 0) {
                g.drawImage(Image.createImage(src_,
                                            s.x(),
                                            s.y(),
                                            s.w(),
                                            s.h(),
                                            0),
                          xDelta_,
                          0,
                          20);
            }
            if(d.w() > 0) {
                g.drawImage(Image.createImage(dst_,
                                            d.x(),
                                            d.y(),
                                            d.w(),
                                            d.h(),
                                            0),
                          0,
                          0,
                          20);
            }
        }
    }
}
